package com.zhdl.network.servertcp;

import cn.hutool.core.util.ByteUtil;
import cn.hutool.core.util.HexUtil;
import com.project.command.instruction.service.InstructionSendService;
import com.project.command.instruction.strategy.abs.AbsAfterControlCommand;
import com.project.command.instruction.strategy.process.data.AfterStrategyParam;
import com.zhdl.common.eventbus.center.EventBusCenter;
import com.zhdl.network.modbus.dto.ModbusDto;
import io.netty.buffer.ByteBuf;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.project.common.constant.Dic.HAND_AFTER_DEFAULT_SERVICE;

/**
 * 手操器默认收到数据处理策略
 *
 * @Author: zhangyy
 * @Email: zhang10092009@hotmail.com
 * @Date: 2023/4/18 10:58:35
 * @Version: v1.0
 * @Modified: 手操器默认收到数据处理策略
 * @Description: 手操器默认收到数据处理策略
 */
@Slf4j
@Service(HAND_AFTER_DEFAULT_SERVICE)
public class HandAfterDefaultService extends AbsAfterControlCommand<ModbusDto, ByteBuf> {
    @Autowired
    private InstructionSendService instructionSendService;

    public String highToLow(String data){
        String s1 = data.substring(0,2);
        String s2 = data.substring(2,4);
        return s2 + s1;
    }

    public short stringToHex(String data){
        Integer i = Integer.parseInt(data, 16);
        return i.shortValue();
    }

    public String stringToAscii(String data){

        StringBuilder output = new StringBuilder("");

        for (int i = 0; i < data.length(); i += 2) {
            String str = data.substring(i, i + 2);
            output.append((char) Integer.parseInt(str, 16));
        }
        log.info(output.toString());
        return output.toString();
    }

    public String asciiToString(String data){

        StringBuilder sb = new StringBuilder();
        StringBuilder temp = new StringBuilder();

        // 564e3a322d302e312e34 split into two characters 56, 4e, 3a...
        for (int i = 0; i < data.length() - 1; i += 2) {

            // grab the hex in pairs
            String output = data.substring(i, (i + 2));
            // convert hex to decimal
            int decimal = Integer.parseInt(output, 16);
            // convert the decimal to character
            sb.append((char) decimal);

            temp.append(decimal);
        }
        return sb.toString();
    }


    @Override
    public boolean afterResultJudge(AfterStrategyParam resp) {
        List<byte[]> bytes = resp.getNewData();
        byte[] bytes1 = bytes.get(0);
        String s = HexUtil.encodeHexStr(bytes1);
        EventBusCenter.getEventBus().post(resp);
//        if (s.equals("7777772e7573722e636e")){
//            log.info("客户端心跳不做处理");
//            return true;
//        }else{
//            String substring = s.substring(36, 40);
//            if (substring.equals("0100")) {
//                //设备信息查询结果
//                String idList = s.substring(40, 52);
//                for (int i = 0; i < idList.length()/2; i++) {
//                    log.info("ID"+(i+1)+"_"+HexUtil.hexToInt(idList.substring(2*i,i*2+2)));
//                }
//                String s0 = s.substring(52, 56);
//                String s1 = highToLow(s0);
//                Integer i = Integer.parseInt(s1, 16);
//                log.info("额定电流"+ i.shortValue());
//            } else if (substring.equals("1000")){
//                //实时结果查询
//                String idList = s.substring(40, 56);
//                for (int i = 0; i < idList.length() / 2; i++) {
////                    log.info("日期：" + HexUtil.hexToInt(idList.substring(2*i,i*2+2)));
//                    log.info("日期：" + Integer.parseInt(idList.substring(2*i,i*2+2)));
//                }
//                String s0 = s.substring(56, 60);
//                String s01 = highToLow(s0);
//                log.info("电流有效值"+ stringToHex(s01));
//                String s1 = s.substring(60, 64);
//                String s11 = highToLow(s1);
//                log.info("THDi"+ stringToHex(s11));
//                String s2 = s.substring(64, 68);
//                String s21 = highToLow(s2);
//                log.info("温度"+ stringToHex(s21));
//            } else if (substring.equals("1100")){
//                //实时波形数据查询
//                String idListLength = s.substring(40, 44);
//                String idListDouble = s.substring(44, 48);
//                String s1 = highToLow(idListLength);
//                String s2 = highToLow(idListDouble);
//                int length = HexUtil.hexToInt(s1);
//                int times = HexUtil.hexToInt(s2);
//                String idList = s.substring(48,length*4);
//                String boData = "";
//                for (int i = 0; i < idList.length()/4; i++) {
//                    String idList0 = idList.substring(4*i,4*i+4);
//                    String idList1 = highToLow(idList0);
//                    short i1 = stringToHex(idList1);
//                    boData += ((i1*1000)+"-");
//                    log.info((i+1)+"个波形点为:"+(i1*times));
//                }
//                log.info(boData);
//            } else if (substring.equals("1200")) {
//                //事件数据查询
//                String idList = s.substring(40, 56);
//            } else if (substring.equals("1300")) {
//                //时间数据查询
//                String idList = s.substring(40, 56);
//                String time = "";
//                for (int i = 0; i < (idList.length()-4)/2; i++) {
//                    int temp = Integer.parseInt(idList.substring(2 * i, i * 2 + 2));
//                    time += "-"+temp+"-";
//                }
//                String substring1 = idList.substring(14, 16);
//                int i = HexUtil.hexToInt(substring1);
//                time += i;
//                log.info("时间"+time);
//            } else if (substring.equals("1400")) {
//                //通讯数据查询
//                String idList = s.substring(40, 200);
//
////                String idListF = "";
////                for (int i = 0; i < idList.length()/4; i++) {
////                    String idList0 = idList.substring(4 * i, i * 4 + 4);
////                    String idList1 = highToLow(idList0);
////                    idListF += idList1;
////                }
////                String substring1 = idListF.substring(0, 50);
////                String substring2 = idListF.substring(50, 100);
////                String substring3 = idListF.substring(100, 160);
//
//                String substring1 = idList.substring(0, 50);
//                String substring2 = idList.substring(50, 100);
//                String substring3 = idList.substring(100, 160);
//
//                String s1 = stringToAscii(substring1);
//                String s2 = stringToAscii(substring2);
//                String s3 = stringToAscii(substring3);
//                log.info("+CSQ:" + s1);
//                log.info("+IMEI:" + s2);
//                log.info("+ICCID:" + s3);
//            } else if (substring.equals("6500")) {
//                //阈值定值查询
//                String idList = s.substring(40, 52);
//                String substring1 = idList.substring(0, 4);
//                String s1 = highToLow(substring1);
//                short i1 = stringToHex(s1);
//                log.info("过流判断值" + i1 + "A");
//                String substring2 = idList.substring(4, 8);
//                String s2 = highToLow(substring2);
//                short i2 = stringToHex(s2);
//                log.info("过流判断时间" + i2 + "ms");
//                String substring3 = idList.substring(4, 8);
//                String s3 = highToLow(substring3);
//                short i3 = stringToHex(s3);
//                log.info("通讯模块启动时间间隔" + i3 + "分钟");
//            }
//        }

        return true;

//        ControlInstructionPerformData data = new ControlInstructionPerformData();
//        data.setInstruction(HexUtil.decodeHex("01"));
//        data.setPortKey(resp.getPortKey());
//        data.setProtocols(resp.getProtocols());
//        data.setAfterInstructionName(resp.getAfterInstructionName());
//        data.setDataBit(1);
//        data.setChain("010101");
//        instructionSendService.sendToClient(data);
    }

}
